using AjaxControlToolkit;
using ASP;
using IPMS.AccessAuth;
using IPMS.BLL;
using IPMS.Sort;
using Resources;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class ProductSearch : Page, IRequiresSessionState
{
    protected void btnAdd1_Click(object sender, EventArgs e)
    {
        if (this.txtAdd.Text.Length == 0)
        {
            base.Response.Redirect("Product.aspx");
        }
        else
        {
            base.Response.Redirect("Product.aspx?model=" + this.txtAdd.Text.ToString());
        }
    }

    protected void btnAddProduct_Click(object sender, EventArgs e)
    {
        base.Response.Redirect("Product.aspx");
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Auth canDelete = new Auth(this.Session["UserId"].ToString(), string.Empty, "ProductSearch");
        if (!canDelete.CanDelete())
        {
            this.lblError.Visible = true;
            this.lblError.Text = Messages.mNoDelete.ToString();
        }
        else
        {
            foreach (GridViewRow gvRow in this.gvResults.Rows)
            {
                CheckBox cb = (CheckBox)gvRow.FindControl("cbDelete");
                if ((cb != null) && cb.Checked)
                {
                    try
                    {
                        new ProductProject(this.lblModelHere.Text, this.gvResults.DataKeys[gvRow.RowIndex].Values["ProjectCode"].ToString(), null, null, null).DeleteProductProject();
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                    }
                }
            }
            this.gvResults.DataBind();
            this.btnDelete.Enabled = this.gvResults.Rows.Count != 0;
            this.setButtons();
        }
    }

    protected void ddlProduct_PreRender(object sender, EventArgs e)
    {
        if (!this.Page.IsPostBack)
        {
            try
            {
                if (this.ddlProduct.Items.FindByText("").Text != "")
                {
                }
            }
            catch (NullReferenceException)
            {
                this.ddlProduct.Items.Add("");
            }
            IPSortList.SortByValue(this.ddlProduct);
            this.ddlProduct.SelectedIndex = -1;
            this.ddlProduct.Items.FindByText("").Selected = true;
        }
    }

    protected void ddlProduct_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.lblModelHere.Text = this.ddlProduct.SelectedValue.ToString();
    }

    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }

    protected void OkButton_Click(object sender, EventArgs e)
    {
        string model = this.lblModelHere.Text.ToString();
        string ownergroupID = this.Session["OwnerGroupID"].ToString();
        foreach (GridViewRow gvRow in this.gvAddProject.Rows)
        {
            CheckBox cb = (CheckBox)gvRow.FindControl("CheckBox1");
            if ((cb != null) && cb.Checked)
            {
                try
                {
                    new ProductProject(model, this.gvAddProject.DataKeys[gvRow.RowIndex].Values["ProjectCode"].ToString(), this.Page.User.Identity.Name.ToString(), this.Page.User.Identity.Name.ToString(), ownergroupID).SetProductProject();
                }
                catch (Exception ex)
                {
                    if (ex.Message.IndexOf("PRIMARY") > 0)
                    {
                        return;
                    }
                    List<string> lstValues = new List<string>();
                    //this.lblError.Text = IPDataFormat.GenerateMessage(ex, lstValues);
                    this.lblError.Text = ex.Message.ToString();
                }
            }
        }
        this.gvResults.DataBind();
        this.btnDelete.Enabled = this.gvResults.Rows.Count != 0;
        this.setButtons();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Auth canView = new Auth(this.Session["UserId"].ToString(), string.Empty, "ProductSearch");
        if (!canView.CanView())
        {
            base.Response.Redirect("ErrorPage/NotAllowed.aspx");
        }
        else
        {
            this.setButtons();
        }
    }

    private void setButtons()
    {
    }
}
